package com.zr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zr.constant.RedisConstant;
import com.zr.dao.SetmealDao;
import com.zr.entity.PageResult;
import com.zr.entity.QueryPageBean;
import com.zr.pojo.Setmeal;
import com.zr.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther zhaorui
 * @date 2019-11-25 - 16:54
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private JedisPool jedisPool;


    //提取设置表关联的方法
    @Override
    public void setCheckgroupIdAndSetmealId(Integer setmealId, Integer[] checkgroupIds){
        if (checkgroupIds.length > 0 && checkgroupIds != null ){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setCheckgroupAndSetmeal(map);
            }
        }
    }


    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);

        String img = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
        Integer setmealId = setmeal.getId();
        /*if (checkgroupIds.length > 0 && checkgroupIds != null ){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setCheckgroupAndSetmeal(map);
            }
        }*/
        setCheckgroupIdAndSetmealId(setmealId,checkgroupIds);

    }

    @Override
    public PageResult QueryPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.findPage(queryString);
        long total = page.getTotal();

        List<Setmeal> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public List<Setmeal> findAllSetmeal() {
        List<Setmeal> list = setmealDao.findAllSetmeal();
        return list;
    }

    @Override
    public Setmeal findById(int id) {
        return  setmealDao.findById(id);
    }
}
